/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.general.range.impl;

import org.kopsox.general.range.Range;

/**
 * Implementation of Range which just allows Integer-Values as Limits
 * 
 * @author Konrad Renner - 12.10.2010
 *
 */
public final class IntegerRange implements Range {
	
	private static final long serialVersionUID = 5506329769622494583L;
	
	private final Integer lowerLimit;
	private final Integer upperLimit;
	private final int hashCode;
	
	public IntegerRange(Integer lowerLimit,Integer upperLimit) {
		
		//one property has to be defined
		if(lowerLimit == null && upperLimit == null) {
			throw new IllegalArgumentException("it is not allowed that both, lowerLimit and upperLimit, are NULL");
		}
		
		this.lowerLimit = lowerLimit;
		this.upperLimit = upperLimit;
		this.hashCode = generateHashCode();
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.range.Range#getLowerLimit()
	 */
	@Override
	public Integer getLowerLimit() {
		return this.lowerLimit;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.range.Range#getUpperLimit()
	 */
	@Override
	public Integer getUpperLimit() {
		return this.upperLimit;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.general.range.Range#isValueWithinTheLimits(java.lang.Object)
	 */
	@SuppressWarnings("boxing")
	@Override
	public boolean isValueWithinTheLimits(Object value) {
		
		if(value instanceof Number) {
			Number val = (Number)value;
			Integer intVal = val.intValue();
			
			return isValueWithinLowerLimit(intVal) && isValueWithinUpperLimit(intVal);
		}
		
		throw new IllegalArgumentException("value has to be an Integer");
	}

	private final boolean isValueWithinLowerLimit(Integer value) {
		return this.lowerLimit==null?true:value.compareTo(this.lowerLimit) >= 0;
	}
	
	private final boolean isValueWithinUpperLimit(Integer value) {
		return this.upperLimit==null?true:value.compareTo(this.upperLimit) <= 0;
	}
	

	private final int generateHashCode() {
		final int prime = 31;
		int result = 17;
		result = prime * result
				+ ((lowerLimit == null) ? 0 : lowerLimit.hashCode());
		result = prime * result
				+ ((upperLimit == null) ? 0 : upperLimit.hashCode());
		return result;
	}

	@Override
	public int hashCode() {
		return this.hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		IntegerRange other = (IntegerRange) obj;
		if (lowerLimit == null) {
			if (other.lowerLimit != null)
				return false;
		} else if (!lowerLimit.equals(other.lowerLimit))
			return false;
		if (upperLimit == null) {
			if (other.upperLimit != null)
				return false;
		} else if (!upperLimit.equals(other.upperLimit))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("IntegerRange:");
		sb.append("lowerLimit=");
		sb.append(this.lowerLimit);
		sb.append(",upperLimit=");
		sb.append(this.upperLimit);
		sb.append(";");
		
		return sb.toString();
	}
}
